package com.atguigu.spzx.cart.service.impl;

import com.atguigu.spzx.cart.service.CartService;
import com.atguigu.spzx.common.utils.AuthContextUtil;
import com.atguigu.spzx.feign.product.ProductFeignClient;
import com.atguigu.spzx.model.entity.product.CartInfo;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.entity.user.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ProductFeignClient productFeignClient;

    @Override
    public void addToCart(Long skuId, Integer skuNum) {
        //根据用户id  商品id 去redis里查询以下有没有该项, 区分是修改还是删除、还是插入？
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        Long userId = userInfo.getId();
        Boolean isHasCart = redisTemplate.opsForHash().hasKey("user:cart:" + userId, skuId + "");
        if (isHasCart){
            //修改已存在商品操作
            CartInfo cartInfo = (CartInfo)redisTemplate.opsForHash().get("user:cart:" + userId, skuId + "");
            cartInfo.setSkuNum(cartInfo.getSkuNum()+skuNum);
            //修改后存入
            redisTemplate.opsForHash().put("user:cart:" + userId,skuId + "",cartInfo);
        }else {
            //添加 需要根据商品ID去查询到所需要的信息
            CartInfo cartInfo = new CartInfo();
            ProductSku productSku= productFeignClient.getSkuById(skuId);
            cartInfo.setCartPrice(productSku.getSalePrice());
            cartInfo.setSkuNum(skuNum);
            cartInfo.setSkuId(skuId);
            cartInfo.setUserId(userId);
            cartInfo.setImgUrl(productSku.getThumbImg());
            cartInfo.setSkuName(productSku.getSkuName());
            cartInfo.setIsChecked(1);
            cartInfo.setCreateTime(new Date());
            cartInfo.setUpdateTime(new Date());
            //添加进redis
            redisTemplate.opsForHash().put("user:cart:" + userId,skuId + "",cartInfo);
        }


    }

    @Override
    public List<CartInfo> getCartList() {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        Long userId = userInfo.getId();
        List<CartInfo> cartInfoList =(List<CartInfo>)redisTemplate.opsForHash().values("user:cart:" + userId);
        return cartInfoList;
    }

    @Override
    public void deleteCartBySkuId(Long skuId) {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        Long userId = userInfo.getId();
        redisTemplate.opsForHash().delete("user:cart:" + userId,skuId+"");
    }

    @Override
    public void checkCart(Long skuId, Integer isChecked) {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        Long userId = userInfo.getId();
        CartInfo cartInfo =(CartInfo)redisTemplate.opsForHash().get("user:cart:" + userId,skuId+"");
        cartInfo.setIsChecked(isChecked);
        redisTemplate.opsForHash().put("user:cart:" + userId,skuId+"",cartInfo);
    }

    @Override
    public List<CartInfo> getAllChecked() {
        //获取到用户数据,根据用户数据去redis里查购物车数据
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        Long userId = userInfo.getId();
        //注意这里查询的是用户所有的购物车项
        List<CartInfo> cartInfoList = (List<CartInfo>)redisTemplate.opsForHash().values("user:cart:" + userId);

        //目前没有接触到redis条件查询,所以我们这里需要排除下没有被checked的购物项

        //下面这行可以用流式编程
        List<CartInfo> checkedCartList=new ArrayList<>();
        for (CartInfo cartInfo : cartInfoList) {
            if(cartInfo.getIsChecked()==1){
                checkedCartList.add(cartInfo);
            }
        }

        return checkedCartList;
    }

    @Override
    public void deleteCheckedCart() {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        System.out.println(userInfo);
        //这里不做redis条件删除  ,这里假删除
        System.out.println("删除用户选中购物车信息(下单完选中商品后做清除处理)");
    }
}
